home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 …SCII & the Runetime Code / ADC Developer CD (1992-07) (''Butch ASCII And The Runtime Code'')_iso / Dev.CD 199207.iso / Tools & Apps / OS⁄Toolbox / Apple Events / AE Word Services 1.0d6 / Writeswell Jr. Source / TestBed.c < prev   
Encoding:
C/C++ Source or Header  |  1992-04-22  |  11.0 KB  |  594 lines  |  [TEXT/KAHL]

  1.  
  2. /* TestBed.c
  3. SPELLSWELL IAC Testbed
  4. */
  5.  
  6. #include <EPPC.h>
  7. #include <AppleEvents.h>
  8. #include <AEObjects.h>
  9. #include "AERegistry.h"
  10. #include "WordServices.h"
  11. #include "TestBed.h"
  12. #include "TBConstants.h"
  13. #include "MyFiles.h"
  14. #include "GenHandlers.h"
  15. #include "AppEvents.h"
  16. #include "AEObj.h"
  17. #include "InitMenu.h"
  18. #include "Gripe.h"
  19. #include "Scroll.h"
  20. #include "Prefs.h"
  21. #include "DoChecking.h"
  22. #include "ServiceMgr.h"
  23. #include "ObWind.h"
  24. #include "ObText.h"
  25. #include "ObNull.h"
  26.  
  27. #define GLOBALS_HERE
  28. #include "TBGlobals.h"
  29. #undef GLOBALS_HERE
  30.  
  31. Boolean DoMouseDown( EventRecord *eventPtr );
  32. Boolean DoMenuCommand( long menuSpot );
  33. Boolean DoAppleMenu( short theItem );
  34. Boolean DoFileMenu( short theItem );
  35. Boolean DoEditMenu( short theItem );
  36. Boolean DoServMenu( short theItem );
  37. Boolean DoKeyDown( long message, short modifiers );
  38. Boolean DoActivateEvt( short modifiers );
  39. Boolean DoUpdateEvt( long message );
  40. void DoAboutMe( void );
  41. Boolean DoOSEvt( EventRecord *eventPtr );
  42. void GrowDocWindow( WindowPtr theWindow, Point where );
  43. void SetTESize( WindowPtr theWindow );
  44. void MyDrawGrowIcon( WindowPtr theWindow );
  45. void UnloadStuff( void );
  46.  
  47. void main( void )
  48. {
  49.     short foo;
  50.  
  51.     MaxApplZone();
  52.     InitGraf(&thePort);
  53.     InitFonts();
  54.     /* FlushEvents(everyEvent, 0); */
  55.     InitWindows();
  56.     TEInit();
  57.     InitDialogs(0L);
  58.     
  59.     gAppFileRefNum = CurResFile();
  60.  
  61.     if ( !OpenPrefFile() ){
  62.         Gripe( "\pCannot open Writeswell, Jr. Preferences" );
  63.         ExitToShell();
  64.     }
  65.  
  66.     PutUpMenus();
  67.     InitCursor();
  68.             
  69. #ifdef NEVER
  70.     gSpellerAddr.descriptorType = typeNull;        /* Indicate that we are not "connected" */
  71.     gSpellerAddr.dataHandle = (Handle)NULL;
  72. #endif
  73.  
  74.     if ( InitReqHandlers() )
  75.         ExitToShell();
  76.  
  77.     if ( InitGenericHandlers() )
  78.         ExitToShell();
  79.  
  80.     if ( InitAEObjStuff() )
  81.         ExitToShell();
  82.  
  83.     while ( EventLoop() )
  84.         ;
  85. }
  86.  
  87. Boolean EventLoop( void )
  88. {
  89.     EventRecord theEvent;
  90.     
  91.     UnloadStuff();
  92.  
  93.     WaitNextEvent( everyEvent, &theEvent, 30, (RgnHandle)NULL );
  94.     
  95.  
  96.     if ( gDocWindow )
  97.         TEIdle( (TEHandle)GetWRefCon( gDocWindow ) );
  98.     
  99.     switch( theEvent.what ){
  100.         case nullEvent:
  101.             DoNullEvent( &theEvent );
  102.             break;
  103.         case mouseDown:
  104.             return DoMouseDown( &theEvent );
  105.             break;
  106.         case mouseUp:
  107.             DoMouseUp();
  108.             break;
  109.         case autoKey:
  110.         case keyDown:
  111.             DoKeyDown( theEvent.message, theEvent.modifiers );
  112.             break;
  113.         case updateEvt:
  114.             DoUpdateEvt( theEvent.message );
  115.             break;
  116.         case diskEvt:
  117.             DoDiskEvt();
  118.             break;
  119.         case activateEvt:
  120.             DoActivateEvt( theEvent.modifiers );
  121.             break;
  122.         case osEvt:
  123.             DoOSEvt( &theEvent );
  124.             break;
  125.         case kHighLevelEvent:
  126.             DoHighLevelEvent( &theEvent );
  127.             break;
  128.         default:
  129.             Gripe("\pGot unknown event type" );
  130.             break;    
  131.     }
  132.  
  133.     return true;
  134. }
  135.  
  136. void UnloadStuff( void )
  137. {
  138.     UnloadSeg( MyOpenFile );
  139.     UnloadSeg( DispatchWind );
  140.     UnloadSeg( DispatchTEText );
  141.     UnloadSeg( DispatchNull );
  142.     UnloadSeg( PutUpMenus );
  143.     
  144.     return;
  145. }
  146.  
  147. Boolean DoNullEvent( EventRecord *theEventPtr )
  148. {
  149.     /* One could execute some sort of background task here */
  150.     return true;
  151. }
  152.  
  153. Boolean DoMouseDown( EventRecord *eventPtr )
  154. {
  155.     WindowPtr    theWindow;
  156.     Boolean        result = true;
  157.     Rect        dragRect;
  158.     TEHandle    textH;
  159.     short        partCode;
  160.     ControlHandle    ctlHdl;
  161.     
  162.     switch( FindWindow( eventPtr->where, &theWindow ) ){
  163.         case inDesk:
  164.             break;
  165.         case inMenuBar:
  166.             result = DoMenuCommand( MenuSelect( eventPtr->where ) );
  167.             break;
  168.         case inSysWindow:
  169.             SystemClick( eventPtr, theWindow );
  170.             break;
  171.         case inContent:
  172.             if ( theWindow != FrontWindow() ){
  173.                 SelectWindow( theWindow );
  174.             } else {
  175.                 GlobalToLocal( &(eventPtr->where) );
  176.                 partCode = FindControl( eventPtr->where, theWindow, &ctlHdl );
  177.                 if ( partCode == 0 ){
  178.                     textH = (TEHandle)GetWRefCon( theWindow );
  179.                     
  180.                     gScrollWindow = theWindow;        /* For use by TrackContentClick */
  181.  
  182.                     TEClick( eventPtr->where, eventPtr->modifiers & shiftKey, textH );
  183.                 }else{
  184.                     DoControl( theWindow, ctlHdl, partCode, eventPtr->where );
  185.                 }
  186.             }
  187.             break;
  188.         case inDrag:
  189.             DragWindow( theWindow, eventPtr->where, &screenBits.bounds );    /* NOT correct but works */
  190.             break;
  191.         case inGrow:
  192.             GrowDocWindow( theWindow, eventPtr->where );
  193.             break;
  194.         case inGoAway:
  195.             TEDispose( (TEHandle)GetWRefCon( gDocWindow) );    /* NOT Nice to the user! */
  196.             DisposeWindow( gDocWindow );        
  197.             gDocWindow = (WindowPtr)NULL;
  198.             FSClose( gRefNum );
  199.             break;
  200.     }
  201.     return result;
  202. }
  203.  
  204. Boolean DoMenuCommand( long menuSpot )
  205. {
  206.     short theMenu;
  207.     short theItem;
  208.     Boolean result;
  209.         
  210.     theMenu = HiWord( menuSpot );
  211.     
  212.     theItem = LoWord( menuSpot );
  213.     
  214.     switch ( theMenu ){
  215.         case 0:
  216.             result = true;
  217.             break;
  218.         case kAppleMenuID:
  219.             result = DoAppleMenu( theItem );
  220.             break;
  221.         case kFileMenuID:
  222.             result = DoFileMenu( theItem );
  223.             break;
  224.         case kEditMenuID:
  225.             result = DoEditMenu( theItem );
  226.             break;
  227.         case kServMenuID:
  228.             result = DoServMenu( theItem );
  229.             break;
  230.     }
  231.     
  232.     HiliteMenu( 0 );
  233.     return result;
  234. }
  235.  
  236. Boolean DoAppleMenu( short theItem )
  237. {
  238.     Handle appleHandle;
  239.     Str255 itemName;
  240.     
  241.     switch ( theItem ){
  242.         case kAMAboutMe:
  243.             DoAboutMe();
  244.             break;
  245.         case kAMDash:
  246.             break;
  247.         default:
  248.             appleHandle = GetResource( 'MENU', kAppleMenuID );
  249.             if ( !appleHandle ){
  250.                 Gripe( "\pCannot get handle to apple menu" );
  251.                 return false;
  252.             }
  253.             
  254.             GetItem( appleHandle, theItem, itemName );
  255.             OpenDeskAcc( itemName );
  256.             if ( gDocWindow ){
  257.                 SetPort( gDocWindow );
  258.             }
  259.             break;
  260.     }
  261.     return true;
  262. }
  263.  
  264. Boolean DoFileMenu( short theItem )
  265. {
  266.     switch ( theItem ){
  267.         case kFMNew:
  268.             if ( gDocWindow )
  269.                 return true;
  270.             
  271.             return MakeNewWindow() == noErr;
  272.             break;
  273.         case kFMQuit:
  274.             return false;
  275.     }
  276.     
  277.     return true;
  278. }
  279.  
  280. Boolean DoEditMenu( short theItem )
  281. {
  282.     TEHandle textH;
  283.  
  284.     if ( !SystemEdit( theItem - 1 ) ){
  285.         if ( gDocWindow ){
  286.             textH = (TEHandle)GetWRefCon( gDocWindow );
  287.             
  288.             switch( theItem ){
  289.                 case kEMCut:
  290.                     TECut( textH );
  291.                     break;
  292.                 case kEMCopy:
  293.                     TECopy( textH );
  294.                     break;
  295.                 case kEMPaste:
  296.                     TEPaste( textH );
  297.                     break;
  298.                 case kEMClear:
  299.                     TEDelete( textH );
  300.                     break;
  301.             }
  302.             
  303.             SetVertScroll( gDocWindow, gVertScroll );
  304.         }
  305.     }
  306.     return true;
  307. }
  308.  
  309. Boolean DoServMenu( short theItem )
  310. {
  311.     OSErr err;
  312.  
  313.  
  314. #ifdef NEVER
  315.     switch( theItem ){
  316.         case kSMSpellCheck:
  317.             DoSpellCheckAll();
  318.             break;
  319.         case kSMSpellCheckSel:
  320.             break;
  321.         case kSMGramCheck:
  322.             break;
  323.         case kSMGramCheckSel:
  324.             break;
  325.     }
  326. #endif
  327.  
  328.     switch( theItem ){
  329.         case kSMCheckSel:
  330.             ToggleSelectCheck();
  331.             break;
  332.         case kSMNewBatch:
  333.             err = GetNewBatchService();
  334.             if ( err )
  335.                 Gripe( "\pGetNewBatchService failed" );
  336.             break;
  337.         case kSMNewInteractive:
  338.             if ( !gDocWindow )
  339.                 return true;
  340.             break;
  341.         case kSMRemoveService:
  342.             break;
  343.         case kSMCheckWord:
  344.             if ( !gDocWindow )
  345.                 return true;
  346.             break;
  347.         case kSMDash:
  348.             break;
  349.         default:
  350.             /* Here is where we actually do some spellchecking! */
  351.             if ( !gDocWindow )
  352.                 return true;
  353.             
  354.             DoSpellCheck( theItem - kSMDash );
  355.             
  356.             break;
  357.     }
  358.     return true;
  359. }
  360.     
  361. Boolean DoMouseUp( void )
  362. {
  363.     return true;
  364. }
  365.  
  366. Boolean DoKeyDown( long message, short modifiers )
  367. {
  368.     char theChar;
  369.     
  370.     theChar = message & charCodeMask;
  371.     
  372.     if ( modifiers & cmdKey )
  373.         DoMenuCommand( MenuKey( theChar ) );
  374.     else {
  375.         if ( gDocWindow == FrontWindow() ){
  376.             TEKey( theChar, (TEHandle)GetWRefCon( gDocWindow ) );
  377.         }
  378.     }
  379.         
  380.     return true;
  381. }
  382.  
  383. Boolean DoUpdateEvt( long message )
  384. {
  385.     GrafPtr    curPort;
  386.     
  387.     BeginUpdate( (WindowPtr)message );
  388.     
  389.     if ( (WindowPtr)message == gDocWindow ){
  390.         GetPort( &curPort );
  391.         SetPort( gDocWindow );
  392.         EraseRect( &( thePort->portRect ) );
  393.         TEUpdate( &( thePort->portRect ), (TEHandle)GetWRefCon( (WindowPtr)message ) );
  394.         MyDrawGrowIcon( (WindowPtr)message );
  395.         DrawControls( gDocWindow );
  396.         SetPort( curPort );
  397.     }
  398.  
  399.     EndUpdate( (WindowPtr)message );
  400.     return true;
  401. }
  402.  
  403. void MyDrawGrowIcon( WindowPtr theWindow )
  404. {
  405.     RgnHandle    clipRgn;
  406.     Rect        newClip;
  407.     
  408.     /* This will fudge the DrawGrowIcon routine so we only draw the outline for the
  409.      * vertical scroll bar.  It's easier than writing a custom WDEF.
  410.      * We have to set a new clip region because the update region will mask off some
  411.      * of what we might want to draw.
  412.      *
  413.      * thePort must be the window that is getting the icon drawn in it.
  414.      */
  415.  
  416.     clipRgn = NewRgn();
  417.     if ( !clipRgn )
  418.         return;
  419.     
  420.     GetClip( clipRgn );
  421.     
  422.     newClip.top = theWindow->portRect.top;
  423.     newClip.right = theWindow->portRect.right;
  424.     newClip.bottom = theWindow->portRect.bottom;
  425.     newClip.left = newClip.right - 15;
  426.     
  427.     ClipRect( &newClip );
  428.     
  429.     DrawGrowIcon( theWindow );
  430.  
  431.     SetClip( clipRgn );
  432.     
  433.     return;
  434. }
  435.  
  436. Boolean DoDiskEvt( void )
  437. {
  438.     return true;
  439. }
  440.  
  441. Boolean DoActivateEvt( short modifiers )
  442. {
  443.  
  444.     if ( gDocWindow ){
  445.  
  446.         if ( modifiers & activeFlag ){
  447.             TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
  448.         } else {
  449.             TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
  450.         }
  451.     }
  452.     return true;
  453. }
  454.  
  455. void GrowDocWindow( WindowPtr theWindow, Point where )
  456. {
  457.     long    newSize;
  458.     short    newWidth;
  459.     short    newHeight;
  460.     GrafPtr    curPort;
  461.     Rect    sizeRect;
  462.     
  463.     SetRect( &sizeRect, 80, 80, 32767, 32767 );
  464.  
  465.     newSize = GrowWindow( theWindow, where, &sizeRect );
  466.     if ( newSize == 0L )
  467.         return;
  468.  
  469.     newWidth = (short) newSize;
  470.     newHeight = (short) ( newSize >> 16 );
  471.     
  472.     GetPort( &curPort );
  473.     SetPort( theWindow );
  474.  
  475.     SizeWindow( theWindow, newWidth, newHeight, true );
  476.     
  477.     SetTESize( theWindow );
  478.  
  479.     /* We have to redraw the whole window, since the grow icon, scroll bars, and
  480.      * text rectange have all changed
  481.      */
  482.     
  483.     InvalRect( &( theWindow->portRect ) );
  484.     
  485.     SizeVertScroll();
  486.     SetVertScroll( theWindow, gVertScroll );
  487.  
  488.     SetPort( curPort );
  489.  
  490.     return;
  491. }
  492.  
  493. Boolean DoOSEvt( EventRecord *eventPtr )
  494. {
  495.     Boolean inBackground;
  496.     
  497.     switch ((eventPtr->message >> 24) & 0x0FF) {     /* high byte of message */
  498.         case suspendResumeMessage:              /* suspend/resume is also an activate/deactivate */
  499.             if ( gDocWindow ){
  500.  
  501.                 inBackground = (eventPtr->message & resumeFlag) == 0;
  502.                 if (inBackground) {
  503.                     ZeroScrap();
  504.                     TEToScrap();
  505.                     if ( gDocWindow ){
  506.                         TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
  507.                     }
  508.                 } else {
  509.                     TEFromScrap();
  510.                     if ( gDocWindow ){
  511.                         TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
  512.                     }
  513.                 }
  514.             }
  515.             
  516.         default:
  517.             break;
  518.     }
  519.     return true;
  520. }
  521.  
  522. Boolean DoHighLevelEvent( EventRecord *theEventPtr )
  523. {
  524.     OSErr err;
  525.     
  526.     err = AEProcessAppleEvent( theEventPtr );
  527.  
  528.     if ( err ){
  529.         Gripe( "\pApple Event Processing Failed" );
  530.         return false;
  531.     }
  532.  
  533.     return true;
  534. }
  535.  
  536. void DoAboutMe( void )
  537. {
  538.     DialogPtr dlg;
  539.     short item;
  540.     
  541.     dlg = GetNewDialog( kAboutMeID, (Ptr)NULL, (WindowPtr) -1 );
  542.  
  543.     if ( !dlg )
  544.         return;
  545.     
  546.     do {
  547.         ModalDialog( (ProcPtr)NULL, &item );
  548.     } while ( item != 1 );
  549.     
  550.     DisposDialog( dlg );
  551.     
  552.     return;
  553. }
  554.  
  555. void SetTESize( WindowPtr theWindow )
  556. {
  557.     Rect    txRect;
  558.     TEHandle    hTE;
  559.     short    rectOffset;
  560.     
  561.     GetTERect( &( theWindow->portRect ), &txRect );
  562.     
  563.     hTE = (TEHandle)GetWRefCon( theWindow );
  564.     
  565.     rectOffset = (*hTE)->destRect.top - (*hTE)->viewRect.top;
  566.  
  567.     (*hTE)->viewRect = txRect;
  568.  
  569.     txRect.top += rectOffset;
  570.     txRect.bottom += rectOffset;
  571.  
  572.     (*hTE)->destRect = txRect;
  573.  
  574.     TECalText( hTE );
  575.  
  576.     return;
  577. }
  578.  
  579. void GetTERect( Rect *portRectPtr, Rect *txRectPtr )
  580. {
  581.     /* Return the rect for the textedit field that will fit in the
  582.      * given portRect, allowing for the scroll bar, and a little bit
  583.      * of margin around all the edges.
  584.      */
  585.  
  586.     *txRectPtr = *portRectPtr;
  587.     InsetRect( txRectPtr, 4, 0 );
  588.     
  589.     txRectPtr->right -= 16;
  590.  
  591.     return;
  592. }
  593.  
  594.